import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientPostParam {
    public static void main(String[] args) {


        URI uri = null;
        try{
            uri = new URIBuilder()
                    .setScheme("http")
                    .setHost("localhost")
                    .setPort(8888)
                    .setPath("/format/date2")
                    .build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        HttpEntity entity= null;
        try {
            final ObjectMapper mapper = new ObjectMapper();
            Map<String,Object> params = new HashMap<>();
            params.put("age","100");
            params.put("name","abc");
            params.put("date",System.currentTimeMillis()+"");

            entity = new StringEntity(mapper.writeValueAsString(params),"utf-8");
        }catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        httpPost.setEntity(entity);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(1000)//建立连接超时时间
                .setConnectionRequestTimeout(5000)//请求超时时间
                .setSocketTimeout(4000)//请求数据读写超时时间
                .setRedirectsEnabled(false)//是否允许重定向
                .build();
        httpPost.setConfig(requestConfig);
        CloseableHttpResponse response = null;
        try(CloseableHttpClient client = HttpClientBuilder.create().build()){

            response = client.execute(httpPost);
            String result = EntityUtils.toString(response.getEntity());
            System.out.println(result);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
